﻿using UnityEngine;

namespace zserialize
{
public class SerializableFieldContainerScriptableObject: ScriptableObject, ISerializableFieldContainer
{
    [HideInInspector]
    public byte[] data;
    public Object[] unityObjs;
    private FieldDictionary fields;

    public FieldDictionary GetFieldDictionary()
    {
        Init();
        return fields;
    }

    public void Clear()
    {
        data = null;
        unityObjs = null;
        if(fields != null)
        {
            fields.Clear();
            fields = null;
        }
    }

    void Init()
    {
        if (data == null)
        {
            return;
        }

        if (fields != null)
        {
            return;
        }

        fields = SerializableFieldContainerHelper.ReadFromBytes(data, unityObjs);
    }
}
}